home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "gl.h"
- #include "stdio.h"
- #include "demograph.h"
-
- /* color cube for fast indexing of rgb values*/
- static Colorindex colorcube[512];
-
- /* set TRUE when colorcube initialized */
- static int cminited;
- static int inRGB = FALSE;
-
- /* functions in this file */
- void init_cmRGB(void);
- Colorindex to16(short rgb[3], short palette[16][3]);
- void make16map(short palette[16][3]);
- void setcolor(int r, int g, int b);
-
-
- /* ************************************************************************
- Init_cmRGB must be called before cmRGB is used. This function builds an
- 8x8x8x2 r g b color cube. There are 512 entries of pairs of indices from
- the lower 16 bits of the SGI 3.2 color map that, when dithered, match as
- close as possible the r g b values of the color cube.
- ************************************************************************ */
- void init_cmRGB(void)
- {
- register short r, g, b;
- register Colorindex *mapptr;
- short c[3];
- short palette[16][3];
-
- cminited = TRUE;
-
- if (getgdesc(GD_BITS_NORM_DBL_RED) == 0 ||
- getgdesc(GD_BITS_NORM_DBL_BLUE) == 0 ||
- getgdesc(GD_BITS_NORM_DBL_GREEN) == 0) {
- cmode();
- gconfig();
- } else {
- RGBmode();
- gconfig();
- inRGB = TRUE;
- return;
- }
-
- /* make array of lower 16 color values */
- make16map(palette);
-
- /* fill in color cube */
- mapptr = &colorcube[0];
- for (b = 0; b < 8; b++) {
- for (g = 0; g < 8; g++) {
- for (r = 0; r < 8; r++) {
- /* 3 bits each = 7R,7G,7B - scale 255 values down to 7 */
- c[0] = r * 255.0 / 7.0;
- c[1] = g * 255.0 / 7.0;
- c[2] = b * 255.0 / 7.0;
- *mapptr++ = to16(c, palette);
- }
- }
- }
- }
-
-
- /* ************************************************************************
- To16 recieves an rgb array and returns the closest values from the bottom 16
- colors of the SGI colormap (palette).
- ************************************************************************ */
- Colorindex to16(rgb,palette)
- short rgb[];
- short palette[][3];
- {
- register int i, dr, dg, db, bestdist, s, bestmatch;
-
- bestdist = 1 << 30;
- for (i = 0; i < 16; i++) {
- /* if color matches return color without calculating distance */
- if (palette[i][0] == rgb[0] &&
- palette[i][1] == rgb[1] &&
- palette[i][2] == rgb[2]) {
- return ((Colorindex) i);
- }
- /* get distance to color */
- dr = palette[i][0] - rgb[0];
- dg = palette[i][1] - rgb[1];
- db = palette[i][2] - rgb[2];
- s = (dr * dr + dg * dg + db * db);
-
- /* pick the shortest distance */
- if (s < bestdist) {
- bestmatch = i;
- bestdist = s;
- }
- }
- return ((Colorindex) bestmatch);
- }
-
-
- /* *************************************************************************
- This routine builds a palette array for the rgb values of the lower 16 bits
- of the color map. It is hard wired because the default colors for this part
- of the map are supposedly fixed. I didn't read colors from the color map as
- some application may have altered these
- ************************************************************************* */
- void make16map(palette)
- short palette[][3];
- {
- short i;
-
- for (i = 0; i < 8; i++) {
- palette[i][0] = (i & 0x0001) * 255;
- palette[i][1] = ((i & 0x0002) >> 1) * 255;
- palette[i][2] = ((i & 0x0004) >> 2) * 255;
- }
-
- palette[8][0] = 85;
- palette[8][1] = 85;
- palette[8][2] = 85;
- palette[9][0] = 198;
- palette[9][1] = 113;
- palette[9][2] = 113;
- palette[10][0] = 113;
- palette[10][1] = 198;
- palette[10][2] = 113;
- palette[11][0] = 142;
- palette[11][1] = 142;
- palette[11][2] = 56;
- palette[12][0] = 113;
- palette[12][1] = 113;
- palette[12][2] = 198;
- palette[13][0] = 142;
- palette[13][1] = 56;
- palette[13][2] = 142;
- palette[14][0] = 56;
- palette[14][1] = 142;
- palette[14][2] = 142;
- palette[15][0] = 170;
- palette[15][1] = 170;
- palette[15][2] = 170;
- }
-
-
- /* ************************************************************************
- This is the routine all the setup was dome for. This routine sets an RGB
- color if the machine is in RGB mode. It sets a colormap color if the
- machine is in color map mode
- ************************************************************************ */
- void setcolor(r,g,b)
- int r,g,b;
- {
- /* set color mode and initialize palette if necessary */
- if (!cminited) {
- init_cmRGB();
- }
-
- if (inRGB) {
- /* set RGBcolor if in RGB mode */
- RGBcolor(r, g, b);
- } else {
- /* set colormap color if in colormap mode */
- color(colorcube[((r >> 5) + ((g & 0xe0) >> 2) + ((b & 0xe0) << 1))]);
- }
- }
-
-